package httpproxymiddleware

import (
	"errors"
	"go-gateway-learn/dao"
	"go-gateway-learn/middleware"
	"go-gateway-learn/public"
	"strings"

	"github.com/gin-gonic/gin"
)

func HTTPJWTOAuthTokenMiddleware() gin.HandlerFunc{
	return func (c *gin.Context)  {
		serverInterface, ok := c.Get("service")
		if !ok{
			middleware.ResponseError(c, 2001, errors.New("service not found"))
			c.Abort()
			return
		}
		serviceDetail := serverInterface.(*dao.ServiceDetail)
		token := strings.ReplaceAll(c.GetHeader("Authorization"),"Bearer "," ")

		appMatched := false
		if token != ""{
			claims, err :=public.JwtDecode(token)
			if err != nil{
				middleware.ResponseError(c, 2002, err)
				c.Abort()
				return
			}
			appList := dao.AppManagerHandler.GetAppList()
			for _, appInfo := range appList{
				if appInfo.AppID == claims.Issuer{
					c.Set("app", appInfo)
					appMatched = true
					break
				}
			}
		}
		if serviceDetail.AccessControl.OpenAuth==1 && !appMatched{
			middleware.ResponseError(c, 2002, errors.New("not match valid app"))
			c.Abort()
			return
		}
		c.Next()
	}
}